package com.yunxi.service.dubboService.impl.vehicle;

import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.vehicle.VehicleProduct;
import com.yunxi.service.localService.vehicle.VehicleConfigVerService;
import com.yunxi.service.localService.vehicle.VehicleProductService;
import com.yunxi.vehicle.pojo.VehicleConfigVerVO;
import com.yunxi.vehicle.pojo.VehicleProductVO;
import com.yunxi.vehicle.service.VehicleProductDubboService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 生产车型 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-11
 */
/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class VehicleProductDubboServiceImpl implements VehicleProductDubboService {

    @Autowired
    @Qualifier(value = "vehicleProductServiceImpl")
    VehicleProductService vehicleProductService;

    @Autowired
    @Qualifier(value = "vehicleConfigVerServiceImpl")
    VehicleConfigVerService vehicleConfigVerService;

    @Override
    public VehicleProductVO getById(String id){
    VehicleProduct vehicleProduct = vehicleProductService.getById(id);
    VehicleProductVO vehicleProductVO = new VehicleProductVO();
        BeanUtils.copyProperties(vehicleProduct,vehicleProductVO);
        return vehicleProductVO;
    }

    @Override
    public ApiResult saveOrUpdate(VehicleProductVO vehicleProductVO) {
        VehicleProduct vehicleProduct = new VehicleProduct();
        BeanUtils.copyProperties(vehicleProductVO, vehicleProduct);
        // 国别
        List<VehicleConfigVerVO> countryList = vehicleConfigVerService.listByCountry(vehicleProduct.getVehicleCode());
        if (!CollectionUtils.isEmpty(countryList)) {
            vehicleProduct.setCountry(countryList.get(0).getFeatureCode());
        }
        // 年款
        List<VehicleConfigVerVO> yearList = vehicleConfigVerService.listByYear(vehicleProduct.getVehicleCode());
        if (!CollectionUtils.isEmpty(yearList)) {
            vehicleProduct.setYear(yearList.get(0).getFeatureCode());
        }
        // 电池
        List<VehicleConfigVerVO> batteryPackList = vehicleConfigVerService.listByBattary(vehicleProduct.getVehicleCode());
        if (!CollectionUtils.isEmpty(batteryPackList)) {
            vehicleProduct.setBatteryPackCode(batteryPackList.get(0).getFeatureCode());
        }
        Boolean aBoolean = checkCodeIsExsit(vehicleProductVO);
        if (aBoolean) {
            return ResponseUtil.fail(402, "操作失败,产品代码" + vehicleProductVO.getProductCode() + "已存在");
        }
        return ResponseUtil.ok(vehicleProductService.saveOrUpdate(vehicleProduct));
    }

    /**
     * 检查产品代码是否重复添加
     * @param vehicleProductVO
     * @throws Exception
     */
    private Boolean checkCodeIsExsit(VehicleProductVO vehicleProductVO) throws IllegalArgumentException {
        Boolean isExsit = false;
        if(vehicleProductVO.getProductCode() != null && StringUtils.isNotEmpty(vehicleProductVO.getProductCode())){
            QueryWrapper<VehicleProduct> queryWrapper = new QueryWrapper();
            if(vehicleProductVO.getProductId() != null && StringUtils.isNotEmpty(vehicleProductVO.getProductId())){
                queryWrapper.ne("PRODUCT_ID",vehicleProductVO.getProductId());
            }
            queryWrapper.eq("PRODUCT_CODE",vehicleProductVO.getProductCode());
            List<VehicleProduct> list = vehicleProductService.list(queryWrapper);
            if(list != null && list.size() > 0){
                isExsit = true;
            }
        }
        return isExsit;
    }

    @Override
    public void removeById(String id) {
    vehicleProductService.removeById(id);
    }

    @Override
    public void del(VehicleProductVO vehicleProductVO) {
        vehicleProductService.del(vehicleProductVO);
    }

    @Override
    public List productList() {
        return vehicleProductService.productList();
    }

    @Override
    public PageInfo<VehicleProductVO> findPageByVO(VehicleProductVO vehicleProductVo, Integer pageNum, Integer pageSize) {
      PageInfo<VehicleProduct> vehicleProductList = vehicleProductService.findPageByVO(vehicleProductVo,pageNum,pageSize);
      PageInfo<VehicleProductVO> vehicleProductVOList = new PageInfo<>();
      BeanUtils.copyProperties(vehicleProductList,vehicleProductVOList);
      return vehicleProductVOList;
   }

    @Override
    public void removeByListVo(List<VehicleProductVO> list) {
        vehicleProductService.removeByListVoCus(list);
    }
}
